﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Frogism.Core;
using System.Collections.ObjectModel;


namespace Frogism.Data._2 {
  public class BookRepository : IBookManager{
 
  
    public System.Collections.ObjectModel.Collection<IBook> GetReading() {
      using (NHibernate.ISession session = NHConfig.GetSessionFactory().OpenSession()) {
        IList<Book> templist = session.CreateCriteria<Book>().Add(NHibernate.Criterion.Restrictions.Eq("IsFinished",false)).List<Book>();
        Collection<IBook> newList = new Collection<IBook>();
        foreach (Book b in templist) {
          if (!b.IsFinished && b.Reads.Count() > 0) {
            newList.Add(b);
          }
        }
        return newList.OrderByDescending(b => b.Reads.Max(r => r.ReadDate)).ToCollection();
        //return session.CreateQuery("from Book").List<Book>();

      }
    }
    public IEnumerable<IBook> GetFinished() {
        IEnumerable<IBook> templist = this.GetAll();
        IList<IBook> newList = new List<IBook>();
        foreach (Book b in templist) {
          if (b.IsFinished) {
            newList.Add(b);
          }
        }
        return newList;
    }
    public IEnumerable<IBook> GetAll()
    {
        using (NHibernate.ISession session = NHConfig.GetSessionFactory().OpenSession())
        {
            //return session.CreateCriteria<Domain.Book>().List<Domain.Book>();
            return session.CreateQuery("from Book").List<IBook>();
        }
    }
    public IBook Get(int id) {
      using (NHibernate.ISession session = NHConfig.GetSessionFactory().OpenSession()) {
        return session.Get<Book>(id);
      }
    }
    public void Update(IBook book) {
      using (NHibernate.ISession session = NHConfig.GetSessionFactory().OpenSession()) {
        using (NHibernate.ITransaction trans = session.BeginTransaction()) {
            if (book.Id < 1)
            {
                session.Save(book);
            }
            else
            {
                session.Update(book);
            }
          trans.Commit();
        }
      }
    }

    public void UpdateRead(IBook book, IRead read) {
      using (NHibernate.ISession session = NHConfig.GetSessionFactory().OpenSession()) {
        using (NHibernate.ITransaction trans = session.BeginTransaction()) {
            read.BookId = book.Id;
          if (read.Id <= 0) {
            
            session.Save(read);
          } else {
            session.Update(read);
          }
          trans.Commit();
        }
      }
    }

    public void DeleteRead(IRead read) {
      using (NHibernate.ISession session = NHConfig.GetSessionFactory().OpenSession()) {
        using (NHibernate.ITransaction trans = session.BeginTransaction()) {
          session.Delete(read);
          trans.Commit();
        }
      }
    }

    public IRead GetOrCreateTodaysRead(IBook book) {
      IRead read = book.Reads.SingleOrDefault(r => r.ReadDate == DateTime.Now.CleanDate());
      if (read == null) {
        read = new Read();
        read.Id = -1;
        read.BookId = book.Id;
        read.Page = 0;
        read.ReadDate = DateTime.Now.CleanDate();
      }
      return read;
    }

   public IEnumerable<IBook> GetPublicReading() {
      IEnumerable<IBook> templist = this.GetReading();
      IList<IBook> newList = new List<IBook>();
      foreach (Book b in templist) {
        if (!b.IsPrivate) {
          newList.Add(b);
        }
      }
      return newList;
    }

    public IRead GetReadById(IBook book, int readId) {
     using (NHibernate.ISession session = NHConfig.GetSessionFactory().OpenSession()) {
       return session.Get<Read>(readId);
     }
    }
    
    public System.Collections.ObjectModel.Collection<int> YearList {
      get {
          IEnumerable<IBook> books = this.GetAll();
        IEnumerable<int> years = from b in books select b.GoalYear;
        return years.Distinct().ToCollection();
      }
    }
  }
}
